//
//  WYWelcmeViewController.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/12.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit


class WYWelcmeViewController: UIViewController {
    
    //背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var headImageView: UIImageView = {
    
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        view.layer.cornerRadius = 42.5
        view.layer.masksToBounds = true
        return view
    }()
    
    //文字
    private lazy var messageLabel: UILabel = {
        
        let lable = UILabel()
        
        if let name = WYUserAccountViewModel.sharedUserAccount.userAccount?.name{
        
            lable.text = "欢迎回来,\(name)"
            
        } else {
        
            lable.text = "欢迎回来"
        }
        lable.alpha = 0
        lable.font = UIFont.systemFontOfSize(12)
        lable.textColor = UIColor.darkGrayColor()
        return lable
    }()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view./Users/wangyi/Desktop/Swift/05-iPhone项目-第3天(项目框架搭建)/02-其他资源/素材/Welcome
        
        setUPUI()
    }
    override func viewDidAppear(animated: Bool) {
        startAnimation()
    }
    override func loadView() {
       
        //自定义视图
        view = bgImageView
        view.backgroundColor = UIColor.yellowColor()
    }
    //自动布局
     private func setUPUI() {
        
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(300)
            make.centerX.equalTo(self.view)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
        
    }
    
    //开启动画
    private func startAnimation() {
    
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //执行动画结束
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                //进入动画界面后 就会在1 秒后 通过发送通知 进入主界面 在主界面中 有一个 判断函数
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //进入首页  通知  传值给 AppDelegate 中的 判断switchRootVC 中的值 指向那个 ?
                        //发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
              
      
        }
        
    }
    
}
